﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MyVoteMVC.Common.Objects
{
    public static class SmtpCommand
    {
        private static Dictionary<SmtpCommandType, string> CommandDic
            = new Dictionary<SmtpCommandType, string>(){ 
            {SmtpCommandType.HELLO,"HELO {0}"},
            {SmtpCommandType.EHELLO, "EHLO {0}"},
            {SmtpCommandType.AUTH_LOGIN, "AUTH LOGIN"},
            {SmtpCommandType.MAIL_FROM, "MAIL FROM:<{0}>"},
            {SmtpCommandType.RCPT_TO, "RCPT TO:<{0}>"},
            {SmtpCommandType.DATA, "DATA"},
            {SmtpCommandType.END_DATA, "."},
            {SmtpCommandType.QUIT, "QUIT"}
            };

        public static string GetCommand(string argument, SmtpCommandType commandType)
        {
            return string.Format(CommandDic[commandType], argument);
        }

        public static string GetCommand(SmtpCommandType commandType)
        {
            return CommandDic[commandType];
        }

    }

    public enum SmtpCommandType
    {
        HELLO,
        EHELLO,
        AUTH_LOGIN,
        MAIL_FROM,
        RCPT_TO,
        DATA,
        END_DATA,
        QUIT
    }

    public class SendSmtpException : Exception
    {
        private string _message = "";
        public string SmtpMessage
        {
            get
            {
                return _message;
            }
        }

        public SendSmtpException(string code)
        {
            if (MailCode.ErrorCode.ContainsKey(code))
                _message = MailCode.ErrorCode[code];
            else
                _message = code;
        }

        public SendSmtpException(string code,string address)
        {
            if (MailCode.ErrorCode.ContainsKey(code))
                _message = MailCode.ErrorCode[code];
            else
                _message = code;

            _message = string.Format("发送邮箱地址{0}出错" + _message, address);
        }
    }

    public class MailCode
    {
        public const string OK = "250";
        public const string AUTH = "334";
        public const string AUTH_OK = "235";
        public const string DATA_READY = "354";
        public const string READY = "220";

        public static readonly Dictionary<string, string> ErrorCode
            = new Dictionary<string, string>() {
                {"500", "邮箱地址错误"},
                {"501", "参数格式错误"},
                {"502", "命令不可实现"},
                {"503", "服务器需要SMTP验证"},
                {"504", "命令参数不可实现"},
                {"421", "服务未就绪，关闭传输信道"},
                {"450", "要求的邮件操作未完成，邮箱不可用（例如，邮箱忙）"},
                {"550", "要求的邮件操作未完成，邮箱不可用（例如，邮箱未找到，或不可访问）"},
                {"451", "放弃要求的操作；处理过程中出错"},
                {"551", "用户非本地，请尝试<forward-path>"},
                {"452", "系统存储不足，要求的操作未执行"},
                {"552", "过量的存储分配，要求的操作未执行"},
                {"553", "邮箱名不可用，要求的操作未执行（例如邮箱格式错误）"},
                {"432", "需要一个密码转换"},
                {"534", "认证机制过于简单"},
                {"538", "当前请求的认证机制需要加密"},
                {"454", "临时认证失败"},
                {"530", "需要认证"},
                {"999", "请填写邮件标题"}
            };
        public static readonly Dictionary<string, string> OkCode
            = new Dictionary<string, string>() { 
                {"220", "服务就绪"},
                {"250", "要求的邮件操作完成"},
                {"251", "用户非本地，将转发向<forward-path>"},
                {"354", "开始邮件输入，以<CRLF>.<CRLF>结束"},
                {"221", "服务关闭传输信道"},
                {"334", "服务器响应验证Base64字符串"},
                {"235", "验证成功"}
            };
    }
}
